<!DOCTYPE html>
<html>
<head>
    <title>挂号--中软高科-2015</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>

    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>
    <script type="text/javascript" src="../Js/ckeditor/ckeditor.js"></script>
 

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
    $(function () {       
		$('#backid').click(function(){
				window.location.href="index.html";
		 });
    });
    </script>
</head>
<body>
<form  id="myForm" action="index.html" method="post" class="definewidth m20">
<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <td width="10%" class="tableleft">姓名</td>
        <td><input type="text" name="hosR_name" id="name" value="陆小凤"/></td>
    </tr>

    <tr>
        <td width="10%" class="tableleft">身份证号</td>
        <td><input type="text" name="hosR_idCar" id="idCar" value="21324324324"/></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">挂号费</td>
        <td><input type="text" name="hosR_regPrice" value="5"/>元</td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">社保号</td>
        <td><input type="text" name="hosR_medical" value="453253453"/></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">联系电话</td>
        <td><input type="text" name="hosR_phone" value="13543235678"/></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">是否自费</td>
        <td><input type="radio" name="hosR_selfPrice" value="0" checked/>否&nbsp;&nbsp;&nbsp;
            <input type="radio" name="hosR_selfPrice" value="1"/>是</td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">性别</td>
        <td><input type="radio" name="hosR_sex" value="0" checked/>男&nbsp;&nbsp;&nbsp;
            <input type="radio" name="hosR_sex" value="1"/>女</td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">年龄</td>
        <td><input type="text" name="hosR_age" id="age" value="33"/></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">职业</td>
        <td><input type="text" name="hosR_work" id="work" value="狗仔队"/></td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">初复诊</td>
        <td><input type="radio" name="hosR_lookDoctor" value="0" checked/>初诊&nbsp;&nbsp;&nbsp;<input type="radio" name="hosR_lookDoctor" value="1"/>复诊</td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">所挂科室</td>
        <td><select id="departments" name="hosR_keshi">
            <option>请选择科室</option>
        </select>
        </td>
    </tr>
    <tr>
        <td width="10%" class="tableleft">指定医生</td>
        <td><select id="doctor" name="doctor_id">
            <option>请选择医生</option>
            </select></td>
    </tr>
	<tr>
        <td width="10%" class="tableleft">备注</td>
        <td><textarea name="hosR_remark" id="desc">此人疑拟有精神病</textarea></td>
	</tr>
    <tr>
        <td colspan="2">
			<center>
				<button type="button" id="btnUpdate" class="btn btn-primary" type="button">保存</button> &nbsp;&nbsp;<button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
			</center>
		</td>
    </tr>
</table>
</form>
</body>
</html>
<script>
    String.prototype.GetValue= function(para) {
        let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); return null;
    }
    $(function () {
        //根据地址获取Id,如果null则执行增加，否则执行更新
        let url = document.location.toString();//获取URL
        let hosR_id=url.GetValue("hosR_id");
        queryById(hosR_id);
        loadData2();
        function loadData2(){
            $.ajax({
                type: "GET",
                url: "/departments/getlist",
                data: null,
                dataType:"json",
                success: function(vo){
                    let list = vo.list;
                    let res="";
                    for (var i=0;i<list.length;i++){
                        res+='<option value="'+list[i].d_id+'">'+list[i].d_name+'</option>';
                    }
                    $("#departments").append(res);
                }
            });
        }

        $("#departments").change(function () {
            var d_id = $("option:selected").val();
            $.ajax({
                type: "GET",//这里必须写POST
                url: "/departments/"+d_id,
                data: null,
                dataType:"json",
                success: function(vo){
                    let list = vo.obj.doctorList;
                    let res="";
                    $("#doctor").empty().append('<option>'+"请选择医生"+'</option>');

                    for (var i=0;i<list.length;i++){
                        res+='<option value="'+list[i].doctor_id+'">'+list[i].doctor_name+'</option>';
                    }
                    $("#doctor").append(res);
                }
            });
        })
        function queryById(hosR_id) {
            $.ajax({
                type: "GET",
                url: "/hosRegister/" + hosR_id, //RESTful风格的API定义
                data: "",
                success: function (vo) {
                    let obj = vo.obj;
                    let hosR_name = obj.hosR_name;
                    let idCar = obj.hosR_idCar;
                    let card = obj.hosR_medical;
                    let price = obj.hosR_regPrice;
                    let self = obj.hosR_selfPrice;
                    let sex = obj.hosR_sex;
                    let age = obj.hosR_age;
                    let work = obj.hosR_work;
                    let keshi = obj.hosR_keshi;
                    let doctor_name = obj.doctor_id;
                    let desc = obj.hosR_remark;
                    let phone = obj.hosR_phone;
                    let lookDoctor = obj.hosR_lookDoctor;
                    var hosR_selfPrice=$("input[name='hosR_selfPrice']");
                    for (var i=0;i<hosR_selfPrice.length;i++){
                        if(hosR_selfPrice[i].value==self){
                            hosR_selfPrice[i].checked='checked';
                        }
                    }
                    var hosR_sex=$("input[name='hosR_sex']");
                    /*$("input[name='menu_state']").val(state);*/
                    for (var i=0;i<hosR_sex.length;i++){
                        if(hosR_sex[i].value==sex){
                            hosR_sex[i].checked='checked';
                        }
                    }
                    var hosR_lookDoctor=$("input[name='hosR_lookDoctor']");
                    for (var i=0;i<hosR_lookDoctor.length;i++){
                        if(hosR_lookDoctor[i].value==lookDoctor){
                            hosR_lookDoctor[i].checked='checked';
                        }
                    }
                    $("#phone").val(phone);
                    $("#lookDoctor").val(lookDoctor)
                    $("#name").val(hosR_name);
                    $("#card").val(card);
                    $("#idCar").val(idCar);
                    $("#price").val(price);
                    $("#self").val(self);
                    $("#sex").val(sex);
                    $("#age").val(age);
                    $("#work").val(work);
                    $("#departments").val(keshi);
                    $("#doctor").val(doctor_name);
                    $("#desc").val(desc);
                }
            })
        }
        $("#btnUpdate").click(function () {
            $.ajax({
                type: "POST",//这里必须写POST
                url: "/hosRegister/"+hosR_id,
                data: $("#myForm").serialize()+"&_method=PUT",
                dataType:"json",
                success: function(vo){
                    if(vo.code==200) {
                       alert("更新成功！");
                    }else{
                        alert("更新失败！"+vo.msg);
                    }
                }
            });
        });
    })
</script>